package com.syt.test;

import javafx.beans.binding.NumberExpression;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
 * @Author 祝雨田
 * @Date 2020-06-24 9:16 上午
 * @Function
 */
public class NameUtils {

    /**
     * 	修改班级名称
     *  阿拉伯数字 ->  中文数字
     * @param pendingNumber 需要装换的数字
     * @return 处理结果
     */
    public static String handleClassNumber(String pendingNumber) {

        if (pendingNumber == null) {
            return "";
        }

        pendingNumber = pendingNumber.replaceAll("0", "零");
        pendingNumber = pendingNumber.replaceAll("1", "一");
        pendingNumber = pendingNumber.replaceAll("2", "二");
        pendingNumber = pendingNumber.replaceAll("3", "三");
        pendingNumber = pendingNumber.replaceAll("4", "四");
        pendingNumber = pendingNumber.replaceAll("5", "五");
        pendingNumber = pendingNumber.replaceAll("6", "六");
        pendingNumber = pendingNumber.replaceAll("7", "七");
        pendingNumber = pendingNumber.replaceAll("8", "八");
        pendingNumber = pendingNumber.replaceAll("9", "九");

        return pendingNumber;
    }

    public static String handleClassNumber2(String pendingNumber) {

        StringBuilder str = new StringBuilder();
        
        for (int i = 0; i < pendingNumber.length(); i++) {
            switch (pendingNumber.charAt(i)) {
                case '1':
                    str.append('一');
                    break;
                case '2':
                    str.append('二');
                    break;
                case '3':
                    str.append('三');
                    break;
                case '4':
                    str.append('四');
                    break;
                case '5':
                    str.append('五');
                    break;
                case '6':
                    str.append('六');
                    break;
                case '7':
                    str.append('七');
                    break;
                case '8':
                    str.append('八');
                    break;
                case '9':
                    str.append('九');
                    break;
                case '0':
                    str.append('零');
                    break;
                default:
                    str.append(pendingNumber.charAt(i));
                    break;
            }
        }
        return str.toString();
    }

    /**
     * 设置班播机声音大小
     * @param macId 班播机id
     * @param sound 声音大小 0-5
     * @return 发送的串口通信内容
     */
    public static byte[] getSerialPortSetting(String macId, int sound) throws NumberFormatException{

        if (macId.length() != 8) {
            throw new NumberFormatException("macId 长度不对");
        }

        if (sound < 0 || sound > 5) {
            throw new NumberFormatException("声音设置不正确");
        }

        byte[] setting = new byte[11];

        setting[0] = (byte) 0xEC;
        setting[1] = (byte) 0xEC;
        setting[2] = (byte) 0x01;
        setting[3] = (byte) 0x06;

        int j = 4;
        for (int i = 0; i < macId.length(); i+=2) {
            setting[j] = (byte)Integer.parseInt(macId.substring(i, i+2));
            j++;
        }

        setting[8] = (byte) 0x01;
        setting[9] = (byte) 0x00;
        setting[10] = (byte) 0x0A;
        return setting;
    }

    /**
     * 设置班播机发送的内容
     * @param macId id
     * @param content 中文内容
     * @return 字节码
     */
    public static byte[] getSerialPortContent(String macId, String content) {

        if (macId.length() != 8) {
            throw new NumberFormatException("macId 长度不对");
        }

        if (content.length() > 8) {
            throw new NumberFormatException("声音播报过长");
        }

        byte[] bytes = new byte[25];
        bytes[0] = (byte)0xEC;
        bytes[1] = (byte)0xEC;
        bytes[2] = (byte)0x1F;
        bytes[3] = (byte)0x14;

        int j = 4;
        for (int i = 0; i < macId.length(); i+=2) {
            int num = Integer.parseInt(macId.substring(i, i+2));
            num = num / 16 * 10 + num % 16;
            bytes[j] = (byte) num;
            j++;
        }

        try {
            byte[] gbks = content.getBytes("gbk");
            for (byte gbk : gbks) {
                bytes[j] = gbk;
                j++;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        bytes[24] = (byte) 0x0A;

        return bytes;
    }


    /**
     * 字节转十六进制
     * @param b 需要进行转换的byte字节
     * @return  转换后的Hex字符串
     */
    public static String byteToHex(byte b){
        String hex = Integer.toHexString(b & 0xFF);
        if(hex.length() < 2){
            hex = "0" + hex;
        }
        return hex;
    }


    /**
     * Hex字符串转byte
     * @param inHex 待转换的Hex字符串
     * @return  转换后的byte
     */
    public static byte hexToByte(String inHex){
        return (byte)Integer.parseInt(inHex,16);
    }


    /**
     * 字节数组转16进制
     * @param bytes 需要转换的byte数组
     * @return  转换后的Hex字符串
     */
    public static String bytesToHex(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if(hex.length() < 2){
                sb.append(0);
            }
            sb.append(hex);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        byte[] serialPortSetting = getSerialPortSetting("20032086", 1);
        System.out.println(bytesToHex(serialPortSetting));
    }
}
